package com.learn.authentication.controller;

import com.alibaba.fastjson2.JSONObject;
import com.learn.authentication.pojo.User;
import com.learn.authentication.result.R;
import com.learn.authentication.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author DJJ_F
 * @version 1.0
 * @date 2025/04/06
 * @description
 */
@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;
    @PostMapping("/user/login")
    public R<Object> login(@RequestBody User user) {
        String token = loginService.login(user);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("token", token);
        return R.OK(jsonObject);
    }
    @PostMapping("user/logout")
    public R<Object> logout() {
        loginService.logout();
        return R.OK("退出成功");
    }

    /**
     * 参数实际是一个方法的调用
     * public final boolean hasAuthority(String authority) {
     *     return this.hasAnyAuthority(authority);
     *  }
     *  表明有这个权限才放行
     *  字符串内写字符串要用''
     * @return
     */
    @PreAuthorize("hasAuthority('test')")
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
}
